package com.jin.lazytool.utils;

import com.jin.lazytool.task.JinTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataUtils {

    private static final List<JinTask> TASKS = new ArrayList<>();
    private static Map<String,Object> CONFIG = new HashMap<>();

    private static final String SEPARATOR = System.getProperty("file.separator");
    private static final String PROJECT_URL = System.getProperty("user.dir") + SEPARATOR + "data" + SEPARATOR;
    private static final String PROJECT_URL_FILE = "actions.json";

    private static final String CONFIG_URL_FILE = "config.json";

    public static JinTask getFirstCopyTask(){
        if(TASKS.isEmpty()){
            return null;
        }
        return TASKS.get(0);
    }

    public static List<JinTask> loadData(){
        File file = new File(PROJECT_URL + PROJECT_URL_FILE);
        if(!file.exists()){
            return TASKS;
        }
        List<JinTask> list = JsonUtils.deserialize(file, ArrayList.class);
        TASKS.clear();
        TASKS.addAll(list);
        return TASKS;
    }
    public static Map<String,Object> loadConfig(){
        File file = new File(PROJECT_URL + CONFIG_URL_FILE);
        if(!file.exists()){
            return CONFIG;
        }
        CONFIG = JsonUtils.deserialize(file, HashMap.class);
        return CONFIG;
    }

    public static JinTask addJinTask(String name){
        JinTask task = new JinTask();
        task.setName(name);
        TASKS.add(task);
        return task;
    }

    public static void saveData(){
        String dataJson = JsonUtils.serialize(TASKS);
        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(PROJECT_URL);
            if (!file.exists()) { // 如果父目录不存在，创建父目录
                file.mkdirs();
            }
            file = new File(PROJECT_URL + PROJECT_URL_FILE);
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            boolean t = file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(dataJson);
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveConfig(){
        String dataJson = JsonUtils.serialize(CONFIG);
        // 生成json格式文件
        try {
            // 保证创建一个新文件
            File file = new File(PROJECT_URL);
            if (!file.exists()) { // 如果父目录不存在，创建父目录
                file.mkdirs();
            }
            file = new File(PROJECT_URL + CONFIG_URL_FILE);
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            boolean t = file.createNewFile();
            // 将格式化后的字符串写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            write.write(dataJson);
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Map<String,Object> getConfig(){
        return CONFIG;
    }

    public static JinTask removeTask(JinTask task){
        int i = TASKS.indexOf(task);
        TASKS.remove(task);
        if(!TASKS.isEmpty()){
            int min = Math.min(i, TASKS.size() - 1);
            return TASKS.get(min);
        }
        return null;
    }

    public static boolean checkNameRepeat(String name){
        for (JinTask item : TASKS) {
            if(name.equals(item.getName())){
                return false;
            }
        }
        return true;
    }

    public static List<JinTask> getJinTaskList() {
        return TASKS;
    }

    public static JinTask getTask(String name){
        for (JinTask item : TASKS) {
            if(name.equals(item.getName())){
                return item;
            }
        }
        return null;
    }
}